<?php
	define('MODEL_DISTINCT_FIELD', 'id');	// defines primary distinct table field
	
	class BaseModel extends BaseDataAccess{
		protected $_default_table_name = '';
		
		
		public function get_default_table_name(){
			return $this->_default_table_name;
		}
		
		public function __construct($in_array = array()){
			parent::__construct();
			if(is_array($in_array) && (count($in_array) > 0)){
				$this->from_array($in_array);
			}
		}
		
		/**
		*	assign variables based on array
		*/
		public function from_array($in_array){
			//general::debug('input data', $in_array);
			$vars = get_class_vars(get_class($this));
			//general::debug('from array vars', $vars);
			foreach ($vars as $varname => $varvalue){
				//general::debug('from array varname', $varname);
				if(isset($in_array[$varname])){
					//general::debug('assigned', $in_array[$varname]);
					$this->$varname = $in_array[$varname];	
				}
			}
		}
		/**
		*	make array from current class
		*/
		public function to_array(){
			$result = array();
			$vars = get_class_vars(get_class($this));
			//general::debug('to array vars', $vars);
			foreach ($vars as $varname => $varvalue){
				if ($varname[0] != '_'){
					$result[$varname] = $this->$varname;	
				}
			}
			return $result;
		}
		
		/**
		*	insert self in the table
		*/
		public function insert($table_name = ''){
			$in_data = $this->to_array();
			return parent::insert($in_data, $table_name);
		}
		
		/**
		*	update self in DB
		*/
		public function update($table_name = '', $distinct_field = MODEL_DISTINCT_FIELD){
			$in_data = $this->to_array();
			if (!is_array($distinct_field)){
				$distinct_field = array($distinct_field);
			}
			$where = array();
			foreach($distinct_field as $df){
				$where[$df] = $this->$df;
			}
			return parent::update($in_data, $table_name, $where);
		}
		
		/**
		*	update self in DB, if it's not there, insert new
		*/
		public function insertUpdate($table_name = '', $distinct_field = MODEL_DISTINCT_FIELD){
			$in_data = $this->to_array();
			if (!is_array($distinct_field)){
				$distinct_field = array($distinct_field);
			}
			$where = array();
			foreach($distinct_field as $df){
				$where[$df] = $this->$df;
			}
			$selected = $this->select($table_name, $where, 1);
			if (!empty($selected)){
				return parent::update($in_data, $table_name, $where);
			} else {
				return parent::insert($in_data, $table_name);
			}
		}
		
		
		/**
		*	return models based on select, and make self the first one 
		*/
		public function select($table_name = '', $where = '', $limit = '', $order = ''){
			$result_array = parent::select('*', $table_name, $where, $limit, $order);
			if (is_array($result_array) && count($result_array) > 0){
				$classname = get_class($this);
				$result = array();
				foreach($result_array as $array){
					$result[] = new $classname($array);
				}	
				$this->from_array($result_array[0]);
				return $result;	
			} else {
				return array();
			}
		}
		
		/**
		*	delete self from DB
		*/
		public function delete($table_name = '', $distinct_field = MODEL_DISTINCT_FIELD){
			if (!is_array($distinct_field)){
				$distinct_field = array($distinct_field);
			}
			$where = array();
			foreach($distinct_field as $df){
				$where[$df] = $this->$df;
			}
			return parent::delete($table_name, $where);
		}
		
	}
?>